package com.li.controller;

import com.li.pojo.po.Result;
import com.li.pojo.vo.UploadVo;
import com.li.utile.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class uploadController {

    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 下載pdf文件
     */
    @PostMapping("common/upload")
    public UploadVo uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        //获取文件名后缀
        String substring = filename.substring(filename.lastIndexOf("."));
        //随机文件名
        String name = UUID.randomUUID().toString().replace("-", "");
        //文件夹路径
        String format = new SimpleDateFormat("yyyy/MM/dd").format(new Date());

        String lastname = "/"+ name + substring;
        String url = aliOssUtil.upload(file.getBytes(),lastname);
        String request="api"+lastname;
        return new UploadVo(lastname,200,request);
    }


}
